import {Component, OnInit} from '@angular/core';
import {RechargeService} from '../recharge.service';
import {ActivatedRoute} from '@angular/router';
import {NzMessageService} from 'ng-zorro-antd/message';

@Component({
  selector: 'app-config',
  templateUrl: './config.component.html',
  styleUrls: ['./config.component.scss']
})
export class ConfigComponent implements OnInit {
  tableData: any[] = [];
  editModal = {
    id: '',
    title: '',
    typeName: '',
    brandName: '',
    // pcImg: '',
    // phoneImg: '',
    pcImgB: '',
    pcImgC: '',
    phoneImgB: '',
    phoneImgC: '',
    goodsList: [] as any[],
    status: 0,
    visible: false,
  };
  errImg = '';

  constructor(
    private rechargeService: RechargeService,
    private nzMessageService: NzMessageService,
  ) {
  }

  ngOnInit(): void {
    this.getConfigList();
  }

  getConfigList() {
    this.rechargeService.getConfigList().then((res: any) => {
      console.log(res);
      if (res.code === 0) {
        this.tableData = res.data;
      }
    });
  }

  hideEditModal() {
    this.editModal = {
      id: '',
      title: '',
      typeName: '',
      brandName: '',
      // pcImg: '',
      // phoneImg: '',
      pcImgB: '',
      pcImgC: '',
      phoneImgB: '',
      phoneImgC: '',
      goodsList: [] as any[],
      status: 0,
      visible: false,
    };
  }

  showEdit(data: any) {
    this.editModal = {
      ...data,
      pcImgC: data.pcImg,
      phoneImgC: data.phoneImg,
      visible: true,
    };
  }

  move(data: any, type: 0 | 1) {
    this.rechargeService.moveConfig({id: data.id, type, gid: data.typeSort === 2 ? 15 : 16}).then((res: any) => {
      if (res.code === 0) {
        this.getConfigList();
        this.nzMessageService.success('操作成功');
      }
    });
  }

  changeStatus(id: any, status: 0 | 1) {
    this.rechargeService.editConfig({id, status}).then((res: any) => {
      if (res.code === 0) {
        this.getConfigList();
      }
    });
  }

  save() {
    if (!this.editModal.pcImgB) {
      this.nzMessageService.warning('请选择PC端B端图片');
      return;
    }
    if (!this.editModal.pcImgC) {
      this.nzMessageService.warning('请选择PC端C端图片');
      return;
    }
    if (!this.editModal.phoneImgB) {
      this.nzMessageService.warning('请选择移动端B端图片');
      return;
    }
    if (!this.editModal.phoneImgC) {
      this.nzMessageService.warning('请选择移动端C端图片');
      return;
    }
    this.rechargeService.editConfig({
      id: this.editModal.id,
      // pcImg: this.editModal.pcImg,
      // phoneImg: this.editModal.phoneImg,
      pcImgB: this.editModal.pcImgB,
      pcImgC: this.editModal.pcImgC,
      phoneImgB: this.editModal.phoneImgB,
      phoneImgC: this.editModal.phoneImgC,
      goodsList: this.editModal.goodsList,
      status: this.editModal.status
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.hideEditModal();
        this.getConfigList();
      }
    });
  }
}
